import Line from '../elements/line';
import Circle from '../elements/circle';

import * as _ from '../utils';

export default {
    scatter: function (serie, opts) {
        let elements = [];
        let data = _.getObjProperty(serie, 'data', []);
        let symbol = _.getObjProperty(serie, 'symbol', 'emptyCircle');
        let origin = _.getObjProperty(opts, 'origin', {});
        let xAxis = _.getObjProperty(opts, 'xAxis', []);
        let xAxisData = xAxis.data;
        let yAxisWidth = xAxis.yAxisWidth;

        let yAxis = _.getObjProperty(opts, 'yAxis', {});
        let yAxisData = yAxis.data;
        let xAxisLen = yAxis.xAxisLen;

        let yAxisLen = yAxisData.length;
        let yMax = yAxisData[yAxisLen - 1];

        let x = origin.x;
        let y = origin.y;
        let w = origin.w;
        let h = origin.h;

        let offsetX = x + yAxisWidth;
        let offsetY = y;
        let color = serie.color;
        let lineWidth = opts.lineWidth || 2;
        let dataLen = data.length;
        let xLen = parseInt((w - yAxisWidth) / xAxisLen);

        let lines = [];
        let circles = [];
        for (let i = 0; i < xAxisLen; i++) {
            if (i < dataLen) {
                let val1 = data[i];
                let x1 = parseInt(xLen / 2) + offsetX + xLen * i;
                let y1 = parseInt(((yMax - val1) * h) / yMax) + offsetY;

                circles.push([
                    {
                        x: x1,
                        y: y1,
                        r: lineWidth,
                    },
                ]);
                if (i > 0) {
                    let val2 = data[i - 1];
                    let x2 = x1 - xLen;
                    let y2 = parseInt(((yMax - val2) * h) / yMax) + offsetY;
                    lines.push([
                        {
                            x: x2,
                            y: y2,
                        },
                        {
                            x: x1,
                            y: y1,
                        },
                    ]);
                }
            }
        }

        let circlesLen = circles.length;
        for (let i = 0; i < circlesLen; i++) {
            let points = circles[i];
            let style = {
                strokeStyle: color,
                lineWidth: lineWidth,
                zIndex: 101,
            };
            let action = { closePath: true };
            let circle = new Circle({
                points,
                style,
                action,
            });
            if (symbol !== 'none') {
                elements.push(circle);
            }
        }

        return elements;
    },
};
